<?php
	$conn=mysql_connect("localhost","root","vertrigo") or die("Không thể kết nối!");
	mysql_select_db("webqtd")or die("không tồn tại CSDL\"webqtd\"!");
	mysql_query("SET NAMES 'utf8'"); //Để hiển thị unicode
	session_start();
	$MaSoTietKiem = $_POST["MaSoTietKiem"];
	$MaKH = $_POST["MaKH"];
	$MaLoaiKyHan = $_POST["MaKyHan"];
	
	$HinhThucThangNam = $_POST["HinhThucThangNam"];
	if($HinhThucThangNam == "Thang")
		$sqllaisuatgui = "SELECT * FROM laisuatgui WHERE MaLoaiKyHan = $MaLoaiKyHan AND HangThang = 1";
	else
		$sqllaisuatgui = "SELECT * FROM laisuatgui WHERE MaLoaiKyHan = $MaLoaiKyHan AND HangNam = 1";
	$dslaisuatgui = mysql_query($sqllaisuatgui);
	$donglaisuat = mysql_fetch_array($dslaisuatgui);
	$MaLaiSuatGui = $donglaisuat["MaLaiSuatGui"];
	$SoTienGui = $_POST["SoTienGui"];
	$NgayMoSo = $_POST["NgayMoSo"];
	$NgayDenHan = $_POST["NgayDenHan"];
	
	$NgayLanhLai = "";
	if($NgayMoSo==$NgayDenHan)
	{
		$NgayLanhLai = $NgayMoSo;
		$sqlgetmahtlanhlai = "SELECT * FROM hinhthuclanhlai WHERE ThoiGianLanhLai = 0";
		$dsmahtlanhlai = mysql_query($sqlgetmahtlanhlai);
		$dongmahtlanhlai = mysql_fetch_array($dsmahtlanhlai);
		$MaHTLanhLai = $dongmahtlanhlai["MaHTLanhLai"];
	}
	else 
	{
		$NgayLanhLai = $_POST["NgayLanhLai"];
		$MaHTLanhLai = $_POST["MaHTLanhLai"];
	}
	
	$SoButToan = $_POST["SoButToan"];
	$sotietkiem = new sotietkiembs();
	$sotietkiem->MaSoTietKiem = $MaSoTietKiem;
	$sotietkiem->MaKH = $MaKH;
	$sotietkiem->MaLoaiKyHan = $MaLoaiKyHan;
	$sotietkiem->MaHTLanhLai = $MaHTLanhLai;
	$sotietkiem->MaLaiSuatGui = $MaLaiSuatGui;
	$sotietkiem->SoTienGui = $SoTienGui;
	$sotietkiem->NgayMoSo = $NgayMoSo;
	$sotietkiem->NgayDenHan = $NgayDenHan;
	$sotietkiem->NgayLanhLai = $NgayLanhLai;
	$sotietkiem->SoDuKyHanTT = $SoTienGui;
	$kqsotietkiem = $sotietkiem->ThemSoTietKiem();
	$kekhaigiaodich = new kekhaigiaodichbs();
	$kekhaigiaodich->SoButToan = $SoButToan;
	$kekhaigiaodich->MaLoaiGiaoDich = $_POST["MaLoaiGiaoDich"];
	$kekhaigiaodich->MaNguoiDung = $_POST["MaNhanVien"];
	$kekhaigiaodich->NgayGiaoDich = $NgayMoSo;
	$kekhaigiaodich->SoTienTaiKhoanNo =$SoTienGui;
	$kekhaigiaodich->MaSoTietKiem = $MaSoTietKiem;
	$kqgiaodich = $kekhaigiaodich->Them();
	if($kqsotietkiem && $kqgiaodich)
	{
		$nhatky = new nhatkybs();
		$nhatky->TenSuKien = "Thêm người dùng";
		$nhatky->MaNguoiDung = $_SESSION["MaND"];
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$nhatky->ThoiGian = date("Y-m-d H:m:s", time());
		$nhatky->Them();
		$loaikyhan = new loaikyhanbs();
		$loaikyhan->MaLoaiKyHan = $MaLoaiKyHan;
		$dsloaikyhan = $loaikyhan->LayDanhSach();
		header("Location:index.php?do=inphieuthu&id=$MaSoTietKiem");
	}
	else 
	{
		header("Location:index.php?do=mosotietkiem");
		$_SESSION["LoiMoSoTietKiem"] = "Lỗi: " . mysql_error();
	}
?>